<template>
  <el-card class="box-card">
    <div class='container'>
      <el-form :inline="true" :model="formInline" class="demo-form-inline" ref="refForm">
        <el-form-item label="目录名称">
          <el-input v-model="formInline.directoryName" placeholder=""></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.state" placeholder="请选择">
            <el-option v-for="item,index in status" :key="index" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="eliminate">清除</el-button>
          <el-button type="primary" @click="inquire">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="showAuthDialog = true"><i class="el-icon-edit"></i>新增目录</el-button>
        </el-form-item>
      </el-form>
      <el-alert type="info">
        <i class="el-icon-info"></i>
        数据一共{{total}}条
      </el-alert>
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者">
          </el-table-column>
          <el-table-column label="创建日期">
            <template v-slot='{row}'>
              {{$dateFormat(row.addDate)}}
            </template>
          </el-table-column>
          <el-table-column prop="totals" label="面试题数量">
          </el-table-column>

          <el-table-column prop="state" label="状态" :formatter="formType">
          </el-table-column>

          <el-table-column prop="address" label="操作">
            <template v-slot='{row}'>
              <el-button type="text">
                <span @click="cut(row)">{{row.state===1?'禁用':'启用'}}</span>
              </el-button>
              <!-- row.state === 0 && (row.creatorID === 2 || row.creatorID === 3)? false :true -->
              <template v-if="row.state===0 && row.creatorID === 2">
                <el-button type="text" @click="modification(row)" :disabled=' false '>修改</el-button>
                <el-button type="text" @click="delData(row)" :disabled='false'>删除</el-button>
              </template>
              <template v-if="row.state===0 && row.creatorID === 3">
                <el-button type="text" @click="modification(row)" :disabled='false'>修改</el-button>
                <el-button type="text" @click="delData(row)" :disabled='true'>删除</el-button>
              </template>
              <template v-if="row.state===1 && row.creatorID === 2">
                <el-button type="text" @click="modification(row)" :disabled='true'>修改</el-button>
                <el-button type="text" @click="delData(row)" :disabled='true'>删除</el-button>
              </template>
              <template v-if="row.state===1 && row.creatorID === 3">
                <el-button type="text" @click="modification(row)" :disabled='true'>修改</el-button>
                <el-button type="text" @click="delData(row)" :disabled='true'>删除</el-button>
              </template>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pg.page" :page-sizes="[10, 20, 30, 40]" :page-size="pg.pagesize" layout="->,total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
      <!-- <el-pagination :current-page="pg.page" :page-sizes="[10, 20, 30, 40]" :page-size="pg.pagesize" layout="->,prev, pager, next,sizes, jumper" :total="total" @current-change='handleCurrentChange' background @size-change="handleSizeChange"> -->
      <!-- </el-pagination> -->
      <el-dialog :title="title?'修改目录':'新增目录'" :visible.sync="showAuthDialog" width='500px' @closed='cancel'>
        <el-form label-width="100px" class="demo-ruleForm" :model="catalogueForm" :rules="rules" ref="ruleForm">
          <el-form-item label="所属学科" prop="subjectID">
            <el-select v-model="catalogueForm.subjectID" placeholder="请选择">
              <el-option v-for="item in tableDatas" :key="item.id" :label="item.subjectName" :value="item.subjectID"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input v-model="catalogueForm.directoryName" placeholder="请选择目录名称" class='axx'></el-input>
          </el-form-item>
        </el-form>
        <div class="right">
          <template>
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="addDate">确定</el-button>
          </template>
        </div>

      </el-dialog>
    </div>
  </el-card>

</template>

<script>
import { list, add, remove, detail, update, changeState } from '@/api/hmmm/directorys'
import { status } from '@/api/hmmm/constants'
export default {
  name: 'Directorys',
  data () {
    return {
      tableDatas: [],
      statusx: null,
      status: status,
      sole: {
        id: ''
      },
      title: null,
      formInline: {
        directoryName: '',
        state: ''
      },
      tableData: [],
      pg: {
        page: 1,
        pagesize: 10

      },
      total: 0,
      showAuthDialog: false,
      catalogueForm: {
        subjectID: '',
        directoryName: ''
      },
      subjectName: [],
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getList()
    this.getDate()
  },
  methods: {
    // 操作按钮 启用 禁用
    async cut (row) {
      if (row.state === 1) {
        row.state = 0
        const res = await changeState({
          id: row.id,
          state: row.state
        })
        this.statusx = res.data.success
      } else if (row.state === 0) {
        row.state = 1
        const res = await changeState({
          id: row.id,
          state: row.state
        })
        this.statusx = res.data.success
      }
    },
    // 转换状态
    formType (row) {
      const statusx = [
        {
          value: 1,
          label: '已启用'
        },
        {
          value: 0,
          label: '已禁用'
        }
      ]
      const info = statusx.find(item => parseInt(item.value) === parseInt(row.state))
      if (info) {
        return info.label
      } else {
        return '其他'
      }
    },
    // 查询
    async inquire () {
      try {
        if (this.formInline.directoryName === '' && this.formInline.state === '') {
          this.getList()
        } else {
          const res = await list({
            ...this.formInline,
            ...this.pg
          })

          if (res.status === 200) {
            this.tableData = res.data.items
          }
        }
      } catch {
        this.$message.error('查询失败')
      }
    },
    // 清除
    eliminate () {
      this.formInline.directoryName = ''
      this.formInline.state = ''
    },
    // 取消
    cancel () {
      this.showAuthDialog = false
      this.$refs.ruleForm.resetFields()
      this.title = null
    },
    // 修改
    async modification (row) {
      this.sole.id = row.id
      try {
        this.title = true
        this.showAuthDialog = true
        const res = await detail(this.sole)
        console.log(res)
        if (res.data === []) {
          this.catalogueForm = {
            subjectID: '',
            directoryName: ''
          }
        } else {
          this.catalogueForm = res.data
        }
      } catch {
        this.$message.error('获取信息失败')
      }
    },
    // 删除
    delData (row) {
      this.$confirm('是否删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          const res = await remove({ id: row.id })
          if (res.status === 200) {
            this.$message.success(res.statusText)
            await this.getList()
            if (this.tableData.length === 0 && this.pg.page > 1) {
              this.pg.page--
              this.getList()
            }
          }
        } catch {
          this.$message.error('删除失败')
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 新增
    addDate () {
      this.title = false
      this.$refs.ruleForm.validate(async val => {
        if (val) {
          if (this.title) {
            try {
              const res = await update({
                ...this.catalogueForm,
                ...this.sole
              })
              console.log(res)
              if (res.status === 200) {
                this.showAuthDialog = false
                this.$message.success(res.statusText)
                this.getList()
              }
            } catch {
              this.$message.error('修改失败')
            }
          } else {
            try {
              const res = await add(this.catalogueForm)
              if (res.status === 200) {
                this.showAuthDialog = false
                this.getList()
                this.$message.success(res.statusText)
                this.$refs.ruleForm.resetFields()
              } else {
                this.$message.console.error('新增失败')
              }
            } catch {
              this.$message.console.error('新增失败')
            }
          }
        }
      })
    },
    // 获取列表1
    async getList () {
      const res = await list(this.pg)
      if (res.status === 200) {
        this.tableData = res.data.items
        this.total = res.data.counts
      } else {
        this.$message.error('获取列表信息失败')
      }
    },
    handleCurrentChange (val) {
      this.pg.page = val
      this.getList()
    },
    handleSizeChange (val) {
      this.pg.pagesize = val
      this.getList()
    },
    // 获取列表2
    async getDate () {
      const res = await list({
        page: 1,
        pagesize: 10000
      })
      if (res.status === 200) {
        this.tableDatas = res.data.items
      } else {
        this.$message.error('获取列表信息失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.el-card__body {
  margin: 0 0 0.625rem;
  .el-pagination {
    margin: 0.625rem 0;
  }
}
/deep/ .axx {
  width: 13.75rem !important;
}
.right {
  margin: -0.625rem 0.625rem 0.625rem;
  right: -0.625rem;
  float: right;
}
</style>
